# -*- coding: UTF-8 -*-
"""
HaaS506 RTU Temperature Sensor - Simple Demo
简化版本的温度传感器演示程序
"""

import utime as time
import temperature_sensor


def main():
    """简单的温度传感器演示"""
    print("=== HaaS506 Temperature Sensor Simple Demo ===")

    # 初始化传感器
    if not temperature_sensor.init():
        print("Failed to initialize temperature sensor")
        return

    print("Temperature sensor initialized successfully")
    print("Reading sensor data every 3 seconds...")
    print("Press Ctrl+C to stop\n")

    try:
        loop_count = 0
        while True:
            loop_count += 1

            # 读取温湿度数据
            humidity, temperature = temperature_sensor.read_temperature_humidity()

            if humidity is not None and temperature is not None:
                print(
                    "Loop {}: Temperature: {:.1f} C, Humidity: {:.1f}%RH".format(
                        loop_count, temperature, humidity
                    )
                )
            else:
                print("Loop {}: Failed to read sensor data".format(loop_count))

            # 等待3秒
            time.sleep(3)

    except KeyboardInterrupt:
        print("\nProgram stopped by user")

    finally:
        # 清理资源
        temperature_sensor.cleanup()
        print("Cleanup completed")


if __name__ == "__main__":
    main()
